var mySwiper = null;
var _turnCard = null;

function turnCard(settings) {
	settings = settings || {};
	this.data = settings.data || {}; //初始数据
	this.turned = settings.turned || 0; //已翻数量
	this.max = settings.max || 10; //最大翻牌数量
	this.init();
}
turnCard.prototype.init = function() {
	for (var item in this.data) {
		var card = $(".cards").find(".card" + item);
		if (card.hasClass("cardback")) {
			card.removeClass("cardback");
		}
		card.children().html("<span>X" + this.data[item] + "</span>");
		card.data("data",this.data[item]);
	}
	var that = this;
	$(".hand_bar").bind("click", function() {
		that.turn();
	});
};
turnCard.prototype.turn = function() {
	if (this.max <= this.turned) return; //超过最大翻牌数量
	this.turned++;
	var that=this;
	var card = $(".handler_cards").find(".card").last();    
	var offset=card.offset();
    var random=this.random();
    var targetCard=$(".cards").find(".card"+random);
    var toffset=targetCard.offset();
    
    var $turn=$("<div>",{
    	'class':"card cardback",
    	css:{
    		position:"absolute",
    		bottom:$("body").height()-offset.top-card.height(),
    		left:offset.left,
    		width:card.width()
    	}
    });
    $turn.html("<div></div>");
    $("body").append($turn);
    
    card.hide();
	$(".hand_bar").hide();
    $turn.addClass("turn");
    setTimeout(function(){
    	$turn.addClass("card"+random);
    	$turn.removeClass("cardback");
    	card.show();
	    $(".hand_bar").show();
    },100);
    setTimeout(function(){
    	$turn.width(card.width()*3);
    	$turn.removeClass("turn");
    	$turn.css({
    		"border-radius":"1rem"
    	});
    },1010);
    setTimeout(function(){   	
    	
    	$turn.animate({
    		"left":toffset.left+"px",
    		"bottom":($("body").height()-toffset.top-targetCard.height())+"px",
    		width:targetCard.width()
    	},200,function(){
    		$turn.remove();    	
	    that.add(random);
    	});
    },3000);   

};

turnCard.prototype.random = function() {
	function random(Min,Max){
	var Range = Max - Min;
	var Rand = Math.random();
	var num = Min + Math.round(Rand * Range);
	return num;
	}
	return random(1,6);
};
turnCard.prototype.add=function(index){
	var card=$(".cards").find(".card"+index);
	var data=card.data("data")||0;
	data++;
	if(card.hasClass("cardback")){
		card.removeClass("cardback");		
	}
	card.children().html("<span>X"+data+"</span>");
	card.data("data",data);
};


$(function() {
	
	var data = {
		"1": 2,
		"9": 2
	};
	_turnCard = new turnCard({
		data: data
	});
	$(".view_card").bind("click", function() {
		$(".view_card").hide();
	});
	$("#rule_button").bind("click", function(e) {
		$("#rule").show();
	});
	$("#rule").bind("click", function(e) {
		$("#rule").hide();
	});
	$("#prize_button").bind("click", function(e) {
		$("#prize").show();
	});
	$("#prize").bind("click", function(e) {
		$("#prize").hide();
	});
	$(".more").bind("click", function() {
		$("#friend_list").show();
	});
	$("#friend_list").bind("click", function() {
		$("#friend_list").hide();
	});
	$("#view").bind("click", function() {
		$(".p_list").show();
//		$(".view_card").show();
//		if (!mySwiper) {
//			mySwiper = new Swiper('.swiper-container', {
//				pagination: '.swiper-pagination'
//			});
//		}
	});
	$(".p_list").bind("click", function() {
		$(".qr-box").show();
	});
	$("#join").bind("click", function() {
		$(".share").show();
	});
	$(".share").bind("click", function() {
		$(".share").hide();
	});
	$("#go").bind("click",function(){
		$("#follow").show();
	});
	$("#follow").bind("click",function(){
		$("#follow").hide();
	});
});